package com.shu.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.shu.mmall.common.Result;
import com.shu.mmall.dao.CategoryMapper;
import com.shu.mmall.pojo.Category;
import com.shu.mmall.service.ICategoryService;
import com.shu.mmall.util.EmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author shujinhao
 */
@Slf4j
@Service("categoryService")
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Result addCategory(String categoryName, Integer parentId){
        if (parentId==null || EmptyUtil.isEmpty(categoryName)){
            return Result.createByErrorMessage("新增品类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);

        int rowCount = categoryMapper.insert(category);
        if (rowCount>0){
            return Result.createBySuccess("添加品类成功");
        }
        return Result.createByErrorMessage("添加品类失败");

    }

    @Override
    public Result updateCategoryName(String categoryName, Integer categoryId){
        if (categoryId==null || EmptyUtil.isEmpty(categoryName)){
            return Result.createByErrorMessage("更新品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount>0){
            return Result.createBySuccess("更新品类名字成功");
        }
        return Result.createByErrorMessage("更新品类名字失败");
    }


    @Override
    public Result<List<Category>> getChildrenParallelCategory(Integer categoryId){
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        if (EmptyUtil.isEmpty(categoryList)){
            log.info("未找到当前分类的子类");
        }
        return Result.createBySuccess(categoryList);
    }

    @Override
    public Result getCategoryAndDeepChildrenCategory(Integer categoryId) {
        Set<Category> categorySet= Sets.newHashSet();
        findChildCategory(categorySet,categoryId);

        List<Integer> categoryIdList = Lists.newArrayList();
        if (categoryId!=null){
            for (Category categoryItem : categorySet) {
                categoryIdList.add(categoryItem.getId());
            }
        }
        return Result.createBySuccess(categoryIdList);
    }
    /**
     * 递归算法，算出子节点
     */
    private Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category!=null){
            categorySet.add(category);
        }
        //查找子节点,递归算法一定要有一个退出条件
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for (Category categoryItem : categoryList) {
            findChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }

}
